﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
namespace UcAsp.Net.BLL
{
    public class SubjectsConfig
    {
        private static string path = AppDomain.CurrentDomain.BaseDirectory;
        /// <summary>
        /// 获取产品
        /// </summary>
        public static List<UcAsp.Net.Model.Subjects> GetListSubjects
        {
            get
            {
                if (Comm.SpaceCache.GetCache("GetListSubjects") == null)
                {
                    List<UcAsp.Net.Model.Subjects> list = new List<UcAsp.Net.Model.Subjects>();

                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\Subjects.xml");
                    string xmlpath = ("root/Subject");
                    if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                    {
                        XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                        foreach (XmlNode xns in xmlList)
                        {
                            UcAsp.Net.Model.Subjects model = new UcAsp.Net.Model.Subjects();

                            foreach (XmlNode xn in xns)
                            {
                                XmlToObject(model, xn.Name, xn.InnerText);
                            }
                            list.Add(model);
                        }
                    }
                    Comm.SpaceCache.CreateCache(list,"GetListSubjects");
                }
                return (List<UcAsp.Net.Model.Subjects>)Comm.SpaceCache.GetCache("GetListSubjects");
            }
            
        }
        protected static void XmlToObject(object oPut, string name, string value)
        {
            System.ComponentModel.PropertyDescriptor pd = System.ComponentModel.TypeDescriptor.GetProperties(oPut).Find(name, true);
            if (pd != null)
            {
                if (pd.PropertyType == typeof(int) || pd.PropertyType == typeof(long) || pd.PropertyType == typeof(string) || pd.PropertyType == typeof(DateTime) || pd.PropertyType == typeof(bool))
                {
                    if (pd.PropertyType == typeof(int))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt32(value));
                    }
                    else if (pd.PropertyType == typeof(long))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt64(value));
                    }
                    else if (pd.PropertyType == typeof(double))
                    {
                        pd.SetValue(oPut, System.Convert.ToDouble(value));
                    }
                    else if (pd.PropertyType == typeof(bool))
                    {
                        pd.SetValue(oPut, System.Convert.ToBoolean(value));
                    }
                    else
                    {
                        if (value == null)
                            pd.SetValue(oPut, string.Empty);
                        else
                            pd.SetValue(oPut, value);
                    }
                }
            }
            else
            {
                //对象为空
            }
        }
        public static UcAsp.Net.Model.Subjects GetSubjects(int id)
        {
            UcAsp.Net.Model.Subjects product = GetListSubjects.FirstOrDefault(o => o.SubjectId == id);
            return product;
        }
        public static UcAsp.Net.Model.Subjects GetSubjects(int id, int cid)
        {
            UcAsp.Net.Model.Subjects product = GetCategoriesSubjects(cid).FirstOrDefault(o => o.SubjectId == id);
            return product;
        }
        public static UcAsp.Net.Model.Subjects GetMessageSubjects(int id, int cid)
        {
            UcAsp.Net.Model.Subjects product = GetMessageSubjects(cid).FirstOrDefault(o => o.SubjectId == id);
            return product;
        }
        public static bool SaveSubjects(UcAsp.Net.Model.Subjects model)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\Subjects.xml");
            string xmlpath = ("root/Subject");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);

            foreach (XmlNode xns in xmlList)
            {
                if (xns["SubjectId"].InnerText == model.SubjectId.ToString())
                {
                    xns["SubjectName"].InnerText = model.SubjectName;
                    if (!model.SubjectImg.Equals("nofile"))
                    {
                        xns["SubjectImg"].InnerText = model.SubjectImg;
                    }
                    xns["Categories"].InnerText = model.Categories.ToString();
                    xns["CategoryName"].InnerText = model.CategoryName;
                    xns["Keywords"].InnerText = model.Keywords;
                    xns["MaxNum"].InnerText = model.MaxNum.ToString();
                    xns["WhoView"].InnerText = model.WhoView.ToString();
                }
            }
            xmlDoc.Save(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\Subjects.xml");
            Comm.SpaceCache.RemoveCache("GetListSubjects");
            Comm.SpaceCache.RemoveAllCache();
            return true;
        }

        /// <summary>
        /// 分类页面主题
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<UcAsp.Net.Model.Subjects> GetCategoriesSubjects(int id)
        {
            if (Comm.SpaceCache.GetCache("GetCategoriesSubjects" + id.ToString()) == null)
            {
                List<UcAsp.Net.Model.Subjects> list = new List<UcAsp.Net.Model.Subjects>();
                if (!File.Exists(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\Categories\\" + id.ToString() + "\\Subjects.xml"))
                {
                    return list;
                }
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\Categories\\" + id.ToString() + "\\Subjects.xml");
                string xmlpath = ("root/Subject");
                if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                {
                    XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                    foreach (XmlNode xns in xmlList)
                    {
                        UcAsp.Net.Model.Subjects model = new UcAsp.Net.Model.Subjects();

                        foreach (XmlNode xn in xns)
                        {
                            XmlToObject(model, xn.Name, xn.InnerText);
                        }
                        list.Add(model);
                    }
                }
                Comm.SpaceCache.CreateCache(list, "GetCategoriesSubjects" + id.ToString());
                
            }
            return (List<UcAsp.Net.Model.Subjects>)Comm.SpaceCache.GetCache("GetCategoriesSubjects" + id.ToString());
        }

        public static List<UcAsp.Net.Model.Subjects> GetMessageSubjects(int id)
        {
            if (Comm.SpaceCache.GetCache("GetMessageSubjects" + id.ToString()) == null)
            {
                List<UcAsp.Net.Model.Subjects> list = new List<UcAsp.Net.Model.Subjects>();
                if (!File.Exists(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\ShowMessage\\" + id.ToString() + "\\Subjects.xml"))
                {
                    return list;
                }
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\ShowMessage\\" + id.ToString() + "\\Subjects.xml");
                string xmlpath = ("root/Subject");
                if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                {
                    XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                    foreach (XmlNode xns in xmlList)
                    {
                        UcAsp.Net.Model.Subjects model = new UcAsp.Net.Model.Subjects();

                        foreach (XmlNode xn in xns)
                        {
                            XmlToObject(model, xn.Name, xn.InnerText);
                        }
                        list.Add(model);
                    }
                }
                Comm.SpaceCache.CreateCache(list, "GetMessageSubjects" + id.ToString());
            }
            return (List<UcAsp.Net.Model.Subjects>)Comm.SpaceCache.GetCache("GetMessageSubjects" + id.ToString());
        }
        public static bool SaveCategoriesSubjects(UcAsp.Net.Model.Subjects model, string id)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\Categories\\" + id + "\\Subjects.xml");
            string xmlpath = ("root/Subject");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);

            foreach (XmlNode xns in xmlList)
            {
                if (xns["SubjectId"].InnerText == model.SubjectId.ToString())
                {
                    xns["SubjectName"].InnerText = model.SubjectName;
                    if (!model.SubjectImg.Equals("nofile"))
                    {
                        xns["SubjectImg"].InnerText = model.SubjectImg;
                    }
                    xns["Categories"].InnerText = model.Categories.ToString();
                    xns["CategoryName"].InnerText = model.CategoryName;
                    xns["Keywords"].InnerText = model.Keywords;
                    xns["MaxNum"].InnerText = model.MaxNum.ToString();
                    xns["WhoView"].InnerText = model.WhoView.ToString();
                }
            }
            Comm.SpaceCache.RemoveCache("GetCategoriesSubjects" + id);
            xmlDoc.Save(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\Categories\\" + id + "\\Subjects.xml");
            Comm.SpaceCache.RemoveAllCache();
            return true;
        }
        public static bool SaveMessageSubjects(UcAsp.Net.Model.Subjects model, string id)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\ShowMessage\\" + id + "\\Subjects.xml");
            string xmlpath = ("root/Subject");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);

            foreach (XmlNode xns in xmlList)
            {
                if (xns["SubjectId"].InnerText == model.SubjectId.ToString())
                {
                    xns["SubjectName"].InnerText = model.SubjectName;
                    if (!model.SubjectImg.Equals("nofile"))
                    {
                        xns["SubjectImg"].InnerText = model.SubjectImg;
                    }
                    xns["Categories"].InnerText = model.Categories.ToString();
                    xns["CategoryName"].InnerText = model.CategoryName;
                    xns["Keywords"].InnerText = model.Keywords;
                    xns["MaxNum"].InnerText = model.MaxNum.ToString();
                    xns["WhoView"].InnerText = model.WhoView.ToString();
                }
            }
            Comm.SpaceCache.RemoveCache("GetMessageSubjects" + id);
            xmlDoc.Save(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\ShowMessage\\" + id + "\\Subjects.xml");
            Comm.SpaceCache.RemoveAllCache();
            return true;
        }
    }
}